#include <iostream>
#include <ctime>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <stdio.h>
#define COLS 3
#define ROWS 4
using namespace std;

void perception(const Eigen::Matrix<int,ROWS,COLS> samples);
int main(int argc,char **argv){
   Eigen::Matrix<int,ROWS,COLS> samples_and;
   samples_and<<0,0,0,1,0,0,0,1,0,1,1,1;
   cout<<"samples_and"<<endl;
   cout<<samples_and<<endl;
   Eigen::Matrix<int,ROWS,COLS> samples_or;
   samples_or<<0,0,0,1,0,1,0,1,1,1,1,1;
   cout<<"samples_or"<<endl;
   cout<<samples_or<<endl;
   Eigen::Matrix<int,ROWS,COLS> samples_xor;
   samples_xor<<0,0,0,1,0,1,0,1,1,1,1,0;
   cout<<"samples_xor"<<endl;
   cout<<samples_xor<<endl;
   printf("logical and\n");
   perception(samples_and);
   printf("logical or\n");
   perception(samples_or);
   printf("logical xor\n");
   perception(samples_xor);
}
void perception(const Eigen::Matrix<int,ROWS,COLS> samples){
  Eigen::Vector2d w(1,2);
//  cout<<w<<endl;

  //偏置
 int b=0;
 int  a=1;
 int y;
 int d;
 for(int i=0;i<10;i++){
  for(int j=0;j<ROWS;j++){
      Eigen::VectorXd x(2);
      for(int c=0;c<COLS-1;c++){
            x(c)=samples(j,c);
      }
      d=samples(j,COLS-1);
   //  int delta_b=a*(d-y)
      //cout<<x<<endl;
      //cout<<"w*x+b="<<w.transpose()*x+b<<endl;
      if(w.transpose()*x+b>0){
          y=1;
      }else{
          y=0;
      }
      int delta_b=a*(d-y);
      Eigen::VectorXd delta_w(2);
       delta_w=a*(d-y)*x;
    //  cout<<"delta_w"<<endl;
    // cout<<delta_w<<endl;
    //  printf("epoch %d sample %d [%d,%d,%d,su%d,%d,%d,%d]\n",
     // i,j,w(0),w(1),b,y,delta_w(0),delta_w(1),delta_b);
      cout<<"epoch "<<i<<" sample "<<j<<"["<<w(0)<<" "<<w(1)<<" "<<b<<" "
      <<y<<" "<<delta_w(0)<<" "<<delta_w(1)<<" "<<delta_b<<"]"<<endl;
      w=w+delta_w;
      b=b+delta_b;
     // cout<<endl;
  }
 }
 // cout<<a<<endl;      
}